<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller {
	function __construct() {
        parent::__construct();
        $this->load->library('theme');
        $this->theme->set_template('template');
		$this->theme->set_module('shop');
		$this->theme->set_title('[ Mèo Shop ] Hàng Hiệu Xuất Khẩu');
		$this->load->library('OnlineUsers');
		$this->load->library('visit_counter');
		$this->visit_counter->view();
    }
	public function index() {
		//$page_id = '373209989357244';		
		//$a = $this->visit_counter->getVisitCounter();
		$this->makeSlide();
		$this->getProductNew();		
		$this->shop_render(true);
	}

	public function category($catId) {
		$catId = cut_link($catId);	
		//get category
		$this->load->model('pro_categories_Model', 'category', true);
		$result = $this->category->find_by_primary($catId);
		if(empty($result)) {
			show_404();
		}
		$data['category'] = $result->name;
		$data['catId'] = $catId;
		$this->theme->set_title('[ Mèo Shop ] '.$result->name);
		//get list product by category find_all_by_attr
		$this->load->model('product_Model', 'product', true);
		$result = $this->product->find_all_by_attr('catId', $catId);
		$data['listProduct'] = $result;
		$this->theme->set_widget('list', 'content_right', $data);
		$this->shop_render();
	}

	public function product($productId) {
		$this->load->model('product_Model', 'product', true);
		$result = $this->product->find_all_by_attr('productId', $productId);
		if(empty($result)) {
			show_404();
		}
		$data['product'] = $result[0];
		$this->theme->set_title('[ Mèo Shop ] '.$data['product']->name);
		//get list image
		$data['listImages'] = $this->product->getListImage($data['product']->id);
		$data['showPopup'] = false;
		if($this->config->item('display_popup')) {
			$data['showPopup'] = true;
		}

		$this->theme->set_widget('product', 'content_right', $data);

		//get list product category		
		$catId = $data['product']->catId;
		$this->getListProductSimilar($catId, $productId);
		$this->shop_render();
	}

	public function page($type) {
		$this->load->model('page_Model', 'page', true);
		$data = $this->page->find_by_primary($type);
		$this->theme->set_title($data->title);
		$this->theme->set_widget('page', 'content_right', array('data' => $data));
		$this->shop_render();
	}

	private function getProductNew() {
		$this->load->model('product_Model', 'product', true);
		$result = $this->product->find_all_by_attr('new_flg', 1);
		$data['listProduct'] = $result;
		$data['listTitle'] = 'Hàng mới';
		$this->theme->set_widget('list', 'content_right', $data);
	}

	private function getListProductSimilar($catId, $productId) {
		$this->load->model('product_Model', 'product', true);
		$result = $this->product->getListSimilar($catId, $productId);
		$data['listProduct'] = $result;
		$data['listTitle'] = 'Sản phẩm cùng loại';
		$this->theme->set_widget('list', 'content_right', $data);	
	}
	private function makeSlide() {
		$this->load->model('galleries_Model', 'gallery', true);
		$result = $this->gallery->find_all();
		$this->theme->set_widget('slide', 'content_right', array('slide' => $result));
	}

	private function makeCategories() {
		$this->load->model('pro_categories_Model', 'category', true);
		$result = $this->category->getCategory();
		$this->theme->set_widget('categories', 'content_left', array('category' => $result));
	} 
	private function makeMenu(){
		$this->load->model('pro_categories_Model', 'category', true);
		$result = $this->category->find_all_by_attr('display', '0');
		$aryMenu = array();
		foreach ($result as $value) {
			$aryMenu[$value->parentId][] = $value;
		}
		$this->theme->set_widget('menu', 'menu', array('aryMenu' => $aryMenu));
	}

	private function makeFbBox() {
		$this->theme->set_widget('fb-box', 'content_left');	
	}
	private function shop_render($fb = false) {
		$this->makeMenu();
		$this->makeCategories();
		if($fb) {
			$this->makeFbBox();	
		}
		$this->theme->render();
	}
}